package com.xbongbong.saas.help;

import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.MessageConstant;
import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormEntity;
import com.xbongbong.paas.domain.entity.PaasProcessDataEntity;
import com.xbongbong.paas.domain.entity.ext.PaasProcessNodeTaskEntityExt;
import com.xbongbong.paas.enums.ProcessTypeEnum;
import com.xbongbong.paas.enums.TaskTypeEnum;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.model.PaasProcessDataModel;
import com.xbongbong.paas.model.PaasProcessNodeTaskModel;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.message.constant.ProcessPushConstant;
import com.xbongbong.pro.message.enums.PushTypeEnum;
import com.xbongbong.pro.message.pojo.PushRefTypePojo;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.rabbitmq.producer.BaseProducer;
import com.xbongbong.saas.domain.entity.ext.WorkOrderFormEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.WorkOrderFormModel;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
public class ProcessHelp {

    @Resource
    private PaasProcessDataModel paasProcessDataModel;
    @Resource
    private PaasProcessNodeTaskModel paasProcessNodeTaskModel;
    @Resource
    private WorkOrderFormModel workOrderFormModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private UserModel userModel;
    @Resource
    private BaseProducer baseProducer;

    /**
     *  审批评论@推送消息
     * @param loginUser
     * @param refId
     * @param atUserIds
     * @throws XbbException
     */
    public void pushProcessComment(UserVO loginUser, Long refId, List<String> atUserIds) throws XbbException {
        String corpid = loginUser.getCorpid();
        String userName = loginUser.getName();
        // 移除当前操作人
        atUserIds.remove(loginUser.getUserId());
        PaasProcessDataEntity paasProcessDataEntity = paasProcessDataModel.getByTaskId(refId, corpid);
        if (paasProcessDataEntity == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PROCESS_TASK_NOT_EXIST);
        }

        Map<String, Object> params = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        params.put("del", 0);
        params.put("corpid", corpid);
        params.put("taskId", paasProcessDataEntity.getTaskId());
        params.put("orderByStr", " id DESC ");
        List<PaasProcessNodeTaskEntityExt> nodeTaskList = paasProcessNodeTaskModel.findEntitys(params);
        if (nodeTaskList.isEmpty()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.PROCESS_NODE_TASK_NOT_EXIST);
        }
        Map<String, PaasProcessNodeTaskEntityExt> nodeTaskMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (PaasProcessNodeTaskEntityExt paasProcessNodeTaskEntityExt : nodeTaskList) {
            if (Objects.equals(paasProcessNodeTaskEntityExt.getTaskType(), TaskTypeEnum.UNTREATED.getType())) {
                nodeTaskMap.put(paasProcessNodeTaskEntityExt.getUserId(), paasProcessNodeTaskEntityExt);
            }
        }

        String formName;
        if (Objects.equals(paasProcessDataEntity.getBusinessType(), XbbRefTypeEnum.WORK_ORDER.getCode())) {
            WorkOrderFormEntityExt workOrderFormEntityExt = workOrderFormModel.getByKey(paasProcessDataEntity.getFormId(), corpid);
            if (workOrderFormEntityExt == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
            }
            formName = workOrderFormEntityExt.getName();
        } else {
            PaasFormEntity paasFormEntity = paasFormModel.getByKey(paasProcessDataEntity.getFormId(), corpid);
            if (paasFormEntity == null) {
                throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, MessageConstant.FORM_NOT_EXIST);
            }
            formName = paasFormEntity.getName();
        }
        UserEntity userEntity = userModel.getByKeyIngoreDel(paasProcessDataEntity.getCreatorId(), corpid);
        for (String atUserId : atUserIds) {
            if (nodeTaskMap.containsKey(atUserId)) {
                PaasProcessNodeTaskEntityExt paasProcessNodeTaskEntityExt = nodeTaskMap.get(atUserId);
                PushTypeEnum pushTypeEnum = PushTypeEnum.APPROVAL_COMMENT_AT;
                PushRefTypePojo.Options options = new PushRefTypePojo.Options(paasProcessDataEntity.getAppId(), paasProcessDataEntity.getMenuId(),
                        paasProcessDataEntity.getFormId(), Collections.singletonList(paasProcessDataEntity.getId()), paasProcessDataEntity.getSaasMark(), paasProcessDataEntity.getBusinessType(), refId, paasProcessNodeTaskEntityExt.getId(), ProcessTypeEnum.TODO.getValue());
                MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(loginUser.getCorpid(), Collections.singletonList(atUserId), Collections.singletonList(refId), 0, null,
                        I18nMessageUtil.getMessage(ProcessPushConstant.PROCESS_COMMENT_AT_TITLE), String.format(I18nMessageUtil.getMessage(ProcessPushConstant.PROCESS_COMMENT_AT_CONTENT),userName,userEntity.getName(),formName), null, options);
                baseProducer.sendMessage(pushTypeEnum, messageRabbitMqDTO);
            } else {
                PushTypeEnum pushTypeEnum = PushTypeEnum.APPROVAL_COMMENT_AT;
                PushRefTypePojo.Options options = new PushRefTypePojo.Options(paasProcessDataEntity.getAppId(), paasProcessDataEntity.getMenuId(),
                        paasProcessDataEntity.getFormId(), Collections.singletonList(paasProcessDataEntity.getId()), paasProcessDataEntity.getSaasMark(), paasProcessDataEntity.getBusinessType(), refId, nodeTaskList.get(0).getId(), ProcessTypeEnum.CC.getValue());
                MessageRabbitMqDTO messageRabbitMqDTO = new MessageRabbitMqDTO(loginUser.getCorpid(), Collections.singletonList(atUserId), Collections.singletonList(refId), 0, null,
                        I18nMessageUtil.getMessage(ProcessPushConstant.PROCESS_COMMENT_AT_TITLE), String.format(I18nMessageUtil.getMessage(ProcessPushConstant.PROCESS_COMMENT_AT_CONTENT),userName,userEntity.getName(),formName), null, options);
                baseProducer.sendMessage(pushTypeEnum, messageRabbitMqDTO);
            }
        }
    }

}
